package gob.inei.enaho.controller.activity;

import util.exception.ManejarExcepcion;
import util.android.AndroidUtil;
import util.android.ViewUtil;
import util.form.respuesta.GrupoInput;

import gob.inei.enaho.context.AppContext;
import gob.inei.enaho.context.Preferencias;
import gob.inei.enaho.model.domain.tiempo.MesEnum;
import gob.inei.enaho.model.domain.ubicacion.Odei;
import gob.inei.enaho.model.service.DataXmlMarcoService;
import gob.inei.enaho.model.service.OdeiService;
import gob.inei.enaho2014.R;


import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.SharedPreferences;
import android.os.Bundle;

public class CargarMarcoActivity extends ActionBarActivity 
implements ManejarExcepcion 
{
	
	private GrupoInput f = new GrupoInput(null);
	
	
	private final OdeiService odeiService = AppContext.getServices().getOdeiService();
	private final DataXmlMarcoService dataXmlMarcoService = AppContext.getServices().getDataXmlMarcoService();
	//private ConglomeradoService conglomeradoService = AppContext.getServices().getConglomeradoService(); 
	//private PeriodoService periodoService = AppContext.getServices().getPeriodoService();
	
	private TextView tvConglomerado1;
	private TextView tvConglomerado2;
	private TextView tvViviendaMarco1;
	private TextView tvViviendaMarco2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cargar_marco);
		
		tvConglomerado1 = (TextView) findViewById(R.id.conglomerado1);
		tvConglomerado2 = (TextView) findViewById(R.id.conglomerado2);
		tvViviendaMarco1 = (TextView) findViewById(R.id.vivienda1);
		tvViviendaMarco2 = (TextView) findViewById(R.id.vivienda2);
		
		AndroidUtil.ocultarTeclado(this);
		
		try {
			SharedPreferences sf = Preferencias.leerPreferencias(this);
									
			//Conglomerado c = conglomeradoService.findById(sf.getString(Preferencias.CONGLOMERADO, null));
			Odei odei = odeiService.findById(sf.getString(Preferencias.ODEI, null));
			String anho = sf.getString(Preferencias.ANHO, null);
			MesEnum mes = MesEnum.getEnumByCodigo(sf.getString(Preferencias.MES, null));
			
			ViewUtil.setTextView(this,R.id.etOdei,odei.getCodigo()+" "+odei.getNombre());
			ViewUtil.setTextView(this,R.id.etAnho,anho);
			ViewUtil.setTextView(this,R.id.etMes,mes.getCodigoString()+" "+mes.getNombre());
			
			f.agregar("odei", odei.getCodigo());
			f.agregar("anho", anho);
			f.agregar("mes", mes.getCodigoString());
			
		} catch (Exception e) {
			AndroidUtil.mostrarExcepcionEnToast(getBaseContext(), e);
		}
		
		
		Button button = (Button) findViewById(R.id.cargar);
		button.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {				try {
					new CargarMarcoTask(CargarMarcoActivity.this).execute(f.getRegistro());
					//AndroidUtil.mostrarToast(CargarMarcoActivity.this, f.getValoresJSON());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	public DataXmlMarcoService getDataXmlMarcoService() {
		return dataXmlMarcoService;
	}

	@Override
	public void manejarExcepcion(Exception ex) {
		AndroidUtil.mostrarExcepcionEnToast(getBaseContext(), ex);
	}

	public TextView getTvConglomerado1() {
		return tvConglomerado1;
	}

	public TextView getTvConglomerado2() {
		return tvConglomerado2;
	}

	public TextView getTvViviendaMarco1() {
		return tvViviendaMarco1;
	}

	public TextView getTvViviendaMarco2() {
		return tvViviendaMarco2;
	}

	
	
	
	
}
